package com.ohs.dal.dao.impl;

import java.util.List;

import com.ohs.dal.dao.RoomDAO;
import com.ohs.dal.model.Room;

public class RoomDAOImpl extends AbstractDAOImpl<Room> implements RoomDAO {

	public long saveOrUpdateAndReturn(Room entity) {
		getHibernateTemplate().saveOrUpdate(entity);
		return entity.getRoomId();
	}

	public List<Room> list() {
		return getHibernateTemplate().find("from " + Room.class.getName());
	}

	public Room get(long roomId) {
		return (Room) getHibernateTemplate().get(Room.class, roomId);
	}

	public List<Room> findByLocaiton(long locationId) {
		return getHibernateTemplate().find("from Room r where r.location.locationId = " + locationId);
	}
	
	public List<Room> search(String keyword) {
		String query = "from Room r where r.name like '%" + keyword + "%' or r.info like '%" + keyword + "%' or r.location.locationId in"
				+ " (select l.locationId from Location l where l.address like '%" + keyword + "%')";
		return getHibernateTemplate().find(query);
	}
	
	public boolean isSetupRoom(String username) {
		String query = "from Room r where r.location.account.username = '" + username + "'";
		return (getHibernateTemplate().find(query).size() > 0);
	}
}
